package cn.yanglq.Rss.core.quartz;

import cn.yanglq.Rss.core.rssManage.Rss;
import cn.yanglq.Rss.core.rssManage.RssBean;
import cn.yanglq.Rss.core.rssManage.RssUtil;
import cn.yanglq.Rss.core.util.fileUtil.FileOperation;
import cn.yanglq.Rss.core.util.mailUtil.SendMail;
import cn.yanglq.Rss.web.model.MailSet;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.Calendar;

/**
 * Created by yanglq on 2017/6/6.
 */
public class TestJob implements Job {
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        MailSet set = (MailSet) jobExecutionContext.getJobDetail().getJobDataMap().get("MailSet");
        String url = set.getRssurl();
        String filePath = "D://";
        Rss t = new Rss();
        RssBean rs = null;
        try {
            rs = t.getRSSrsult(url);
        } catch (IOException e) {
            e.printStackTrace();
        }
        String title = null;
        if (rs != null) {
            title = rs.getTitle() + ".doc";
        } else
            title = "未知.doc";
//        System.out.println(rs.getTitle());
        String result = RssUtil.rssBuild(rs).toString();
        FileOperation.contentToTxt(filePath + title, result);
        SendMail sm = new SendMail();
        try {
            sm.sendMail(title, set);
        } catch (IOException | ClassNotFoundException | GeneralSecurityException e) {
            e.printStackTrace();
        }
        System.out.println(Calendar.getInstance().getTime() + "发送成功");

    }
}
